#include "rdwr.h"

void WRITE_comm(void)
{
    printf("kkkkk\n");
    int set = write(client_socket, "reg user", strlen("reg user")); // 发送数据
    if(set < 0) {
        printf("发送失败\n");
    }
    printf("nnnnn\n");
    return NULL;
}

// 管理员登入
bool RDWR_login(char * str)
{
    char sand_buf[1024]  = {0};
    char rever_buf[1024] = {0};
    sprintf(sand_buf, "login,user,%s", str);
    printf("开始登入\n");
    write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据

    // pthread_mutex_lock(&LVGL_lock);       // 上锁
    printf("=======\n");
    int set = read(client_socket, rever_buf, 1024); // 接收数据
    if(set < 0) {
        printf("接收失败\n");
    }
    printf("%s\n", rever_buf);
    if(strstr(rever_buf, "ok") != NULL) {
        printf("登录成功\n");
        // pthread_mutex_unlock(&LVGL_lock); // 解锁
        return true;
    } else {
        printf("登录失败\n");
        // pthread_mutex_unlock(&LVGL_lock); // 解锁
        return false;
    }
}

void * RDWR_get_new(char * rag)
{
    pthread_detach(pthread_self());
    char sand_buf[1024]  = {0};
    char rever_buf[1024] = {0};
    sprintf(sand_buf, "get_new,user");
    printf("获取商品信息\n");
    FILE * fd = fopen("/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/commodity_new.txt", "w");
    if(fd == NULL) {
        printf("文件打开失败\n");
        return NULL;
    }
    write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据

    sleep(1);
    while(1) {
        int set = read(client_socket, rever_buf, 1024); // 接收数据
        if(set < 0) {
            printf("接收失败\n");
        }
        printf("%s\n", rever_buf);
        if(strstr(rever_buf, "end")) {
            fflush(fd);
            printf("获取成功\n");
            pthread_exit(NULL);
        }
        fwrite(rever_buf, 1, strlen(rever_buf), fd);
    }
}

void take_off(char * buf)
{
    printf("buf= %s\n",buf);
    char sand_buf[1024]  = {0};
    sprintf(sand_buf, "take_off,%s", buf);
    int set = write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据
    if(set < 0) {
        printf("发送失败\n");
    }
}

int go_file(char * buf)
{
    char file_name[1024]  = {0};
    sprintf(file_name, "go_file,1001,%s",buf);
    int set = write(client_socket, file_name, strlen(file_name)); // 发送数据
    if(set<0)
    {
        printf("发送失败\n");
    }
    sleep(2);
    char file_path[1024] = {0};
    sprintf(file_path, "/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/commodity_picture/%s",buf);
    FILE *fd = fopen(file_path, "r");
    if(fd == NULL) 
    {
        printf("文件打开失败\n");
        return -1;
    }
    //pthread_mutex_lock(&LVGL_lock);
    while(1)
    {
        char read_buf[128] = {0};
        int set = fread(read_buf, 1, sizeof(read_buf), fd);
        if(set<0)
        {
            //pthread_mutex_unlock(&LVGL_lock);
            printf("文件读取失败\n");
            break;
        }
        if(set==0)
        {
            break;
        }
        printf("buf=\n",read_buf);
        write(client_socket, read_buf, strlen(read_buf));
    }
    sleep(3);
    write(client_socket,"end",strlen("end"));
    printf("fjfdk\n");
}

int go_on(char * buf)
{
    char file_name[1024]  = {0};
    FILE *fd = fopen("/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/go_in", "r");
    if(fd==NULL)
    {
        printf("文件打开失败\n");
        return -1;
    }
    while(1)
    {
        char read_buf[128] = {0};
        char *set = fgets(read_buf,sizeof(read_buf),fd);
        if(set==NULL)
        {
            printf("文件读取失败\n");
            break;
        }
        if(strstr(read_buf,buf))
        {
            char *sand_buf[1024]  = {0};
            sprintf(sand_buf, "go_on,%s", read_buf);
            printf("%s\n",sand_buf);
            write(client_socket, sand_buf, strlen(sand_buf));
            break;

        }
        printf("read_buf=%s\n",read_buf);
    }
  
}